import json
import random
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from user import models,ser
from django.db.models import Q
from utils.jwttool import tool
from utils.tecent import TencentCloudAPI
from utils.sendmsg import send_message
from utils.myedis import redistoll
from .tasks import add,send
from utils.authorization import MyAuthentication
import re
# Create your views here.

class Loginpwd(APIView):
    def post(self,request):

        # api = TencentCloudAPI()
        # result = api.captcha(
        #     request.data.get("ticket"),
        #     request.data.get("randstr"),
        #     request._request.META.get("REMOTE_ADDR"),
        # )
        # print(result)
        account=request.data.get('account')
        password=request.data.get('password')
        info=models.UserModel.objects.filter(Q(username=account)|Q(email=account)|Q(phone=account)).first()
        if info is None:
            return Response({'code':400,'msg':'账户不存在'})
        elif info.password==password:
            data={
                'id':info.id,'account':account,'heading':info.heading,'token':tool.getToken(info.id,account)
            }
            if info.heading:
                data['heading']=info.heading
            else:
                data['heading']="/uploads/2022/1.jpg"
            return Response({'code': 200, 'msg':'登录成功','data':data})
        else:
            return Response({'code':400,'msg':'密码错误'})

class SendCode(APIView):
    def post(self,request):
        phone=request.data.get("phone")
        code=str(random.randint(1000,9999))
        rediscode=redistoll.getstr(phone)
        if rediscode:
            return Response({'code':400,'msg':'请勿重复发送'})
        else:
            send.delay(phone,code)
            redistoll.setstr(phone,code,60*5)
            return Response({'code':200,'msg':'发送成功'})

class Login(APIView):
    def post(self,request):
        account=request.data.get("account")
        code=request.data.get("code")
        rediscode=redistoll.getstr(account)
        if code !=rediscode:
            return Response({'code':400,'msg':'验证码错误'})
        info=models.UserModel.objects.filter(phone=account,isshow=1).first()
        if info is None:
            newdata=models.UserModel.objects.create(phone=account)
            token=tool.getToken(newdata.id,newdata.phone)
            id=newdata.id
            return Response({'code':200,'msg':'登录成功','data':{'token':token,'id':id,'account':account}})
        token=tool.getToken(info.id,info.phone)
        id=info.id
        return Response({'code':200,'msg':'登录成功','data':{'token':token,'id':id,'account':account}})


class regView(APIView):
    def get(self,request,phone):
        data = models.UserModel.objects.filter(phone=phone, isshow=1).first()
        if data is None:
            return Response({'code': 200, 'msg': '手机号不存在，可以注册'})
        else:
            return Response({'code': 400, 'msg': '注册手机号已存在'})
    def post(self,request):
        mobile=request.data.get('mobile')
        password=request.data.get('password')
        re_password=request.data.get('re_password')
        sms_code=request.data.get('sms_code')
        rediscode=redistoll.getstr(mobile)
        if sms_code!=rediscode:
            return Response({'code':400,'msg':'验证码错误'})
        if password!=re_password:
            return Response({'code': 400, 'msg':'两次密码不同'})
        data=models.UserModel.objects.filter(phone=mobile,isshow=1).first()
        if data is None:
            newdata=models.UserModel.objects.create(phone=mobile,password=password)
            token=tool.getToken(newdata.id,newdata.phone)
            id=newdata.id
            return Response({'code':200,'msg':'注册成功','data':{'token':token,'id':id,'mobile':mobile}})
        else:
            return Response({'code': 400, 'msg':'注册手机号已存在'})


class TestUser(APIView):
    def post(self,request):
        sum_ = add.delay(1, 2)#异步任务的调用  通过delay
        print(sum_.get())
        return Response({"code": 200, "msg": "测试成功"})

class Cart(APIView):
    authentication_classes = [MyAuthentication]
    def get(self,reqeust):
        return Response({"code": 200, "msg": "测试成功"})


